﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class AddLegalClientForm : Form
    {
        
        LegalClientForm legalFr;
        OracleConnection connection;
        public AddLegalClientForm(LegalClientForm LegalFr)
        {
            legalFr = LegalFr;
            InitializeComponent();
        }

        private void AddLegalClientForm_Load(object sender, EventArgs e)
        {
        }
        private void SelectFromCredit(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selRate = new ClassRateType();
            selRate = dbWork.SelectFromCreditRate(client_type);
            int j = 0;
            if (selRate.id_rate.Count == 0) 
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
            } 
            else 
            { dataGridView1.RowCount = selRate.id_rate.Count; }
            while (j < selRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selRate.title[j].ToString());
                dataGridView1[1, j].Value = selRate.procent[j].ToString();
                dataGridView1[2, j].Value = selRate.period[j].ToString();
                j++;
            }
            
        }

        private void SelectFromAccumulation(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selRate = new ClassRateType();
            selRate = dbWork.SelectFromAccumulationRate(client_type);
            int j = 0;
            if (selRate.id_rate.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
            }
            else
            { dataGridView1.RowCount = selRate.id_rate.Count; }
            while (j < selRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selRate.title[j].ToString());
                dataGridView1[1, j].Value = selRate.procent[j].ToString();
                dataGridView1[2, j].Value = selRate.period[j].ToString();
                j++;
            }
        }

        private void CleanArrayList()
        {
            
        }

        public void Refresh()
        {
            this.CleanArrayList();
            string client_type = "юридический";
            if (comboBox1.SelectedIndex == 0)
            {
                this.SelectFromCredit(client_type);
            }
            else
            {
                this.SelectFromAccumulation(client_type);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            try
            {
                int index;
                index = dataGridView1.CurrentRow.Index;
                Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                int period = Convert.ToInt32(dataGridView1[2, index].Value);
                int deposit = Convert.ToInt32(textBoxDeposit.Text);
                int capital = Convert.ToInt32(textBoxCapital.Text);
                string INN = maskedTextBox1.Text;
                string org_type = textBoxOrganizationType.Text;
                string founders = textBoxFounder.Text;
                string title = textBoxTitle.Text;
                DBWorkClass dbWork = new DBWorkClass();
                dbWork.InsertFromLegalClient(dateTimePicker1.Value, comboBox1.SelectedIndex, period, deposit, procent, capital, INN, org_type, founders, title);
                
                this.Close();
                legalFr.refreshTable();
            }
            catch (FormatException) { MessageBox.Show("накосячили во входных данных!"); }
        }

        private void buttonCalc_Click(object sender, EventArgs e)
        {
            try
            {
                LogicClass logicObj = new LogicClass();
                int index = dataGridView1.CurrentRow.Index;
                int summa = Convert.ToInt32(textBoxDeposit.Text);

                if (comboBox1.SelectedIndex == 0)
                {
                    Double procent = Convert.ToDouble(dataGridView1[1,index].Value);
                    labelInfo.Text = "сумма долга =";
                    labelItog.Text = logicObj.GetCreditSumm(summa, procent).ToString();
                }
                if (comboBox1.SelectedIndex == 1)
                {
                    int period = Convert.ToInt32(dataGridView1[2, index].Value);
                    Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                    labelInfo.Text = "сумма на счёте=";
                    labelItog.Text = logicObj.UpdateDeposit(summa, period, procent).ToString();
                }

            }
            catch (FormatException) { MessageBox.Show("чё вы там во входных данных накосячили!"); }
        }
    }
}
